import {computed, reactive, ref} from "vue";
import {PayListDAT} from "../../types/responseTypes";
import {getPayList,payRefout} from "../../api/pay";
import {useStore} from "vuex";

export function usePayList(){

    const payListWrap = reactive<{payList: PayListDAT[]}>({
        payList: []
    })

    const total = ref(0)
    const {state} = useStore()
    const CUSER_ID = state.user.userInfo.CUSER_ID
    function initPayList(page: number = 1, pageSize: number = 20, STATE: string){
        return new Promise((resolve, reject) => {
            getPayList({
                CUSER_ID,
                CLASS_ID: '',
                STUDENTNO: '',
				DISC: '',
                STATE,
                PAGEINDEX: page,
                PAGESIZE: pageSize,
                OPERATOR_ID: CUSER_ID
            }).then(res => {
                if (res.ERROR[0].errcode == '0'){
                    payListWrap.payList = JSON.parse(JSON.stringify(res.DAT))
                    total.value = res.COUNT[0].NUM
                    resolve(null)
                }else {
                    uni.showToast({
                        title: res.ERROR[0].error,
                        icon: 'none',
                        mask: true
                    })
                    reject('err')
                }
            }).catch(err => {
                uni.showToast({
                    title: '获取列表失败',
                    icon: 'error',
                    mask: true
                })
                reject('err')
            })
        })
    }
	function initpayRefout(out_trade_no:string){
		return new Promise((resolve,reject)=>{
			payRefout({
				out_trade_no:out_trade_no,
                OPERATOR_ID: state.user.userInfo.CUSER_ID
			})
			.then(res=>{
				if(res.trade_state){
					//查询订单成功
				}
			})
		})
	}
    return {
        total,
        payList: computed(() => payListWrap.payList),
        initPayList,
		initpayRefout
    }
}
